import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.io.DataInputStream;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class TopMenuGUI {
	Etxt2DBGUI parent;
	JFileChooser fc = new JFileChooser();
	
	JMenu menuFile = new JMenu( "File" );
	
	JMenuItem fileNew = new JMenuItem( "New" );
	ActionListener fileNewListener = new ActionListener(  ) {
        public void actionPerformed(ActionEvent event) {
        	 parent.queryTextArea.add(new FileTextArea(parent,15,100),"<new" + parent.queryTextArea.getComponentCount() +">");
        }
    };
	
	
	JMenuItem fileOpen = new JMenuItem( "Open" );
	ActionListener fileOpenListener = new ActionListener(  ) {
        public void actionPerformed(ActionEvent event) {
        	fc.setDialogTitle("Open");
        	int returnVal = fc.showOpenDialog(TopMenuGUI.this.parent);

       		if (returnVal == JFileChooser.APPROVE_OPTION) {
       			File file = fc.getSelectedFile();
       			FileTextArea area = new FileTextArea(parent,file,15,100);
       			parent.queryTextArea.add(area,file.getName());
       			parent.queryTextArea.setSelectedComponent(area);
           	}
        }
    };
    
    
    
    JMenuItem fileSave = new JMenuItem( "Save" );
	ActionListener fileSaveListener = new ActionListener(  ) {
        public void actionPerformed(ActionEvent event) {
        	FileTextArea fta = (FileTextArea)parent.queryTextArea.getSelectedComponent();
        	File currentFile = fta.getFile();
        	if(currentFile == null){
        		fc.setDialogTitle("Save as");
        		int returnVal = fc.showOpenDialog(TopMenuGUI.this.parent);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                	try {
	                	File file = fc.getSelectedFile();
	                    if(!file.exists()){
							file.createNewFile();
	                    }
	                    Writer output = new BufferedWriter(new FileWriter(file));
	                    output.write( fta.getText() );
	                    output.close();
	                    fta.setFile(file);
	                    parent.queryTextArea.setTitleAt(parent.queryTextArea.getSelectedIndex(), file.getName());
                	} catch (IOException e) {
					}
                }
        	}else{
        		try{
        			if(!currentFile.exists()){
        				currentFile.createNewFile();
        			}
        			Writer output = new BufferedWriter(new FileWriter(currentFile));
        			output.write( fta.getText() );
        			output.close();
        			if(parent.queryTextArea.getTitleAt(parent.queryTextArea.getSelectedIndex()).startsWith("*")){
        				parent.queryTextArea.setTitleAt(parent.queryTextArea.getSelectedIndex(), currentFile.getName());
        			}
        		}catch(IOException e){
        			
        		}
        	}
        }
    };
    
    
    
    JMenuItem fileSaveAs = new JMenuItem( "Save as" );
	ActionListener fileSaveAsListener = new ActionListener(  ) {
        public void actionPerformed(ActionEvent event) {
        	FileTextArea fta = (FileTextArea)parent.queryTextArea.getSelectedComponent();
        	File currentFile = fta.getFile();
        	fc.setDialogTitle("Save as");
        	int returnVal = fc.showOpenDialog(TopMenuGUI.this.parent);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
            	try {
	               	File file = fc.getSelectedFile();
	                if(!file.exists()){
						file.createNewFile();
	                }
	                Writer output = new BufferedWriter(new FileWriter(file));
	                output.write( fta.getText() );
	                output.close();
	                fta.setFile(file);
	                parent.queryTextArea.setTitleAt(parent.queryTextArea.getSelectedIndex(), file.getName());
                } catch (IOException e) {
				}
        	}
        }
    };
    

	
    
	JMenuItem fileExit = new JMenuItem( "Exit");
	ActionListener fileExitListener = new ActionListener(  ) {
        public void actionPerformed(ActionEvent event) {
        	parent.resutsTextArea.setText("Exiting Text");
        }
    };
    
    JMenu menuOptions = new JMenu( "Options" );
    
    JMenuItem optionsCorpus = new JMenuItem( "CorpusOptions");
	ActionListener optionsCorpusListener = new ActionListener(  ) {
        public void actionPerformed(ActionEvent event) {
        	JFrame frame = new OptionsCorpusFrameGUI(parent);
        	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        	frame.pack();
        	frame.setVisible(true);
        }
    };

	// Build a help menu
	//JMenu menuHelp = new JMenu( "Help" );
	//JMenuItem helpContents = new JMenuItem( "Contents" );
	//JMenuItem helpAbout = new JMenuItem( "About" );

	// Create a JMenuBar
	JMenuBar menuBar = new JMenuBar();

	public TopMenuGUI(Etxt2DBGUI parent){
		this.parent = parent;
		
		
		menuBar.add( menuFile );
		menuBar.add( menuOptions );

		fileNew.addActionListener(fileNewListener);
		fileOpen.addActionListener(fileOpenListener);
		fileSave.addActionListener(fileSaveListener);
		fileSaveAs.addActionListener(fileSaveAsListener);
		fileExit.addActionListener(fileExitListener);
		optionsCorpus.addActionListener(optionsCorpusListener);
    	// Build the file menu
		menuFile.add( fileNew );
    	menuFile.add( fileOpen );
    	menuFile.add( fileSave );
    	menuFile.add( fileSaveAs );
    	menuFile.addSeparator();
    	menuFile.add( fileExit );
    	menuOptions.add(optionsCorpus);

    	// Add the help menu to the menu bar
    	//menuBar.add( menuHelp );

    	// Build the help menu
    	//menuHelp.add( helpContents );
    	//menuHelp.add( helpAbout );

    	// set the menu bar to our JFrame
    	parent.setJMenuBar( menuBar );
	}
}
